﻿using System;
using System.Threading.Tasks;

namespace DbToPbiSvc.PowerBi.UI
{
    class Program
    {

        // ClientId Microsys 0c51d799-e786-47ae-be80-31370f33ee86

        static void Main(string[] args)
        {

            string clientId = string.Empty;
            string userName = string.Empty;
            string password = string.Empty;
            string operation = string.Empty;

            foreach (var arg in args)
            {
                if (arg.ToLower().StartsWith("operation:"))
                {
                    operation = arg.Split(":".ToCharArray())[1];
                }
                if (arg.ToLower().StartsWith("clientid:"))
                {
                    clientId = arg.Split(":".ToCharArray())[1];
                }
                if (arg.ToLower().StartsWith("username:"))
                {
                    userName = arg.Split(":".ToCharArray())[1];
                }
                if (arg.ToLower().StartsWith("password:"))
                {
                    password = arg.Split(":".ToCharArray())[1];
                }
            }

            if (string.IsNullOrEmpty(operation) || string.IsNullOrEmpty(clientId) || string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password))
            {
                Console.WriteLine("The following parameters are mandatory:");
                Console.WriteLine(" - Operation:{List/Create}");
                Console.WriteLine(" - ClientId:{Client id guid}");
                Console.WriteLine(" - UserName:{User Name}");
                Console.WriteLine(" - Password:{User Password}");
                Console.WriteLine("");
                Console.WriteLine("Note: if Operation is \"Create\" the procedure will read json table definition from tabledef.json file");
            }

            ExecuteOperation(clientId, userName, password, operation).Wait();

        }

        public static async Task ExecuteOperation(string clientId, string userName, string password, string operation)
        {

            string refreshToken = string.Empty;
            string token = Helper.GetAccessToken(clientId, userName, password, ref refreshToken);

            switch (operation.ToLower())
            {
                case "list":

                    PbiDatasets datasets = await Helper.GetDatasets(token);

                    Console.WriteLine("");
                    Console.WriteLine("Dataset List:");

                    foreach (var dataset in datasets.Value)
                    {
                        Console.Write(dataset.Name);
                        Console.Write(": ");
                        Console.WriteLine(dataset.Id);
                    }

                    break;

                case "create":

                    var jsonStatement = System.IO.File.ReadAllText("tabledef.json");

                    Helper.CreateDataset(token, jsonStatement).Wait();

                    break;
            }

        }
    }
}
